package com.ruike.alisurface.services;

import android.app.IntentService;
import android.content.Intent;
import android.os.Environment;

import com.ruike.alisurface.MyApplication;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.bean.advBean;
import com.ruike.alisurface.utils.MYUtiles;
import com.voodoo.lib_frame.base.BaseView;
import com.voodoo.lib_frame.httpTools.DownloadUtil;
import com.voodoo.lib_utils.FileUtils;
import com.voodoo.lib_utils.GsonUtils;
import com.voodoo.lib_utils.L;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品图片下载类
 */
public class MyShopImgService extends IntentService implements BaseView {

    public MyShopImgService() {
        super("MyShopImgService");
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        strpicList.clear();
        urllist.clear();
        mCount = 0;
        list.clear();
        try {
            L.i("-----downImgFile-开始解析商品数据--Count-" + mCount);
            showImgList(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    List<String> strpicList = new ArrayList<>();
    int mCount = 0;
    List<String> urllist = new ArrayList<>();
    String filepath = Environment.getExternalStorageDirectory()
            + File.separator + "picture" + File.separator + "goods_img";
    List<String> list = new ArrayList<>();
    boolean isImgNext = true;

    /**
     * 下载
     */
    public void showImgList(Intent intent) {
        try {
            ArrayList<String> imgshop = intent.getStringArrayListExtra("ShopImglist");
            //生成当前的数据
            if (imgshop != null && imgshop.size() > 0) {
                for (int intI = 0; intI < imgshop.size(); intI++) {
                    String p1 = imgshop.get(intI) ;
                    if (!p1.equals("") && p1 != null && !p1.equals("null")) {
                        if (!list.contains(p1)) {
                            list.add(p1);
                            String Name = p1.substring(p1.lastIndexOf("/")).substring(1);
                            strpicList.add(Name);
                        }
                    }
                }
                L.i("-----downvdeioFile---下载商品图片 list大小="+list.size());
                doImgdownload();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 进行图片的本地判断和下载
     */
    public void doImgdownload() {
        if (mCount >= list.size()) {
            L.i("-----downvdeioFile---下载商品图片 已经循环完了");
            return;
        }
        isImgNext = true;
        for (int i = 0; i < list.size(); i++) {
            if (isImgNext) {
                File myFile = new File(filepath, strpicList.get(mCount));
                if (!myFile.exists()) {
                    isImgNext = false;
                    downImgFile(list.get(mCount), 1 + "");
                }
                mCount++;
                if (mCount >= list.size()) {
                    L.i("-----downvdeioFile---下载商品图片 循环完了");
                    EventBus.getDefault().post(new MsgEventBus("商品图片", "下载商品图片完成"));
                    isImgNext = false;
                    return;
                }
            }
        }
    }

    String uploadUrl1;

    //下载ship
    public void downImgFile(final String strNameList, String type) {
        try {
            L.i("-----downvdeioFile---下载商品图片");
            uploadUrl1 =strNameList;
            urllist.add(uploadUrl1);
            String Name = uploadUrl1.substring(uploadUrl1.lastIndexOf("/")).substring(1);
            DownloadUtil.getInstance().download(uploadUrl1, filepath, Name, this);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onStarted(String loadingStr) {

    }

    @Override
    public void onLoading(long total, long current) {

    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onErrorTip(String errorStr) {

    }

    @Override
    public void onSuccess(String requestUrl, String requestJsonStr) {

    }

    @Override
    public void onFailure(String requestUrl, String requestMsg) {

    }

    @Override
    public void onError(String requestUrl, String errorMsg) {

    }

    @Override
    public void onError(String requestUrl, String errorMsg, String data) {

    }

    @Override
    public void onException(Exception exception) {

    }

    @Override
    public void onDownloadSuccess(String requestUrl, File file) {
        if (urllist.contains(requestUrl)) {
            L.i(mCount + "---商品图片下载成功 ----" + file.getAbsolutePath());
            doImgdownload();
        }
    }

    @Override
    public void onDownloading(String requestUrl, long total, long progress) {

    }

    @Override
    public void onDownloadFailed(String requestUrl, Exception e) {
        L.i(mCount+"---商品图片下载失败-Exception-requestUrl--" + requestUrl
                + "======" + e.toString());
        doImgdownload();
    }
}